<?php
class ControllerCommonSeoUrl extends Controller {
    public function index() {        
        if (isset($this->request->get['_route_'])) {    
            $parts = explode('/', $this->request->get['_route_']);
                                    
            if(isset($parts[0]) && $parts[0] == 'article'){
                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($parts[1]) . "'");
                        
                if ($query->num_rows) {
                    $url = explode('=', $query->row['query']); 
                    $this->request->get['content_id'] = $url[1];
                    $this->request->get['route'] = 'content/item/article';                       
                }         
            }               
            elseif(isset($parts[0]) && $parts[0] == 'categories'){
                $this->request->get['route'] = 'product/categories';    
            }   
			elseif(isset($parts[0]) && $parts[0] == 'manufacturers'){
                $this->request->get['route'] = 'product/manufacturer';    
            }			
            elseif(isset($parts[0]) && $parts[0] == 'manufacturer_category'){                 
                $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($parts[1]) . "'");
                        
                if ($query->num_rows) {
                    $url = explode('=', $query->row['query']); 
                    $this->request->get['path'] = $url[1];
                    $this->request->get['route'] = 'product/manufacturer/category';                       
                }   
            }   
            elseif(isset($parts[0]) && $parts[0] == 'search'){
                $this->request->get['route'] = 'product/search';    
            }                      
            elseif(isset($parts[0]) && $parts[0] == 'contact'){
                $this->request->get['route'] = 'information/contact';    
            } 
            elseif(isset($parts[0]) && $parts[0] == 'list_article'){
                $this->request->get['route'] = 'content/list';    
            }
            elseif(isset($parts[0]) && $parts[0] == 'terms_and_conditions'){
                $this->request->get['route'] = 'information/forhandlen';    
            }
            elseif(isset($parts[0]) && $parts[0] == 'newsletter'){
                $this->request->get['route'] = 'newsletter/newsletter';    
            }
            elseif(isset($parts[0]) && $parts[0] == 'giftcard'){
                $this->request->get['route'] = 'checkout/giftcard';    
            }
            else{    
                $this->load->model('catalog/category');
                $parent_id = 0;                 
                foreach ($parts as $part) {
                    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");
                    
                    if ($query->num_rows) {
                        $url = explode('=', $query->row['query']);    
                        
                        if ($url[0] == 'category_id') {
                            $parent_id = $url[1];
                            
                            if (!isset($this->request->get['path'])) {
                                $this->request->get['path'] = $url[1];
                            } 
                            else {
                                $this->request->get['path'] .= '_' . $url[1];
                            }
                            
                            $categories = $this->model_catalog_category->getCategories($url[1]);                            
                            if(!empty($categories)){
                                $this->request->get['route'] = 'product/categories';
                            }
                            else{
                                $this->request->get['route'] = 'product/category';   
                            }                            
                        }    
                             
                        if ($url[0] == 'product_id') {
                            $this->request->get['product_id'] = $url[1];
                            $this->request->get['route'] = 'product/product';                        
                        }
                                            
                        if ($url[0] == 'manufacturer_id') {
                            /*
                            if($path_url){
                                $this->request->get['manufacturer_id'] = $url[1];  
                                $this->request->get['route'] = 'product/manufacturer/viewManufacturer';  
                            } 
                            */
                            $this->request->get['manufacturer_id'] = $url[1];                        
                            $this->request->get['route'] = 'product/manufacturer/viewManufacturer';     
                        }
                        
                        if ($url[0] == 'information_id') {
                            $this->request->get['information_id'] = $url[1];           
                            $this->request->get['route'] = 'information/information';
                        }  
                        
                        if ($url[0] == 'content_id') {
                            $this->request->get['content_id'] = $url[1];
                            $this->request->get['route'] = 'content/item';  
                        }
                    }                     
                    else {
                        $this->request->get['route'] = 'error/not_found';    
                    }
                }
            } 
           
            if (isset($this->request->get['route'])) {
                return $this->forward($this->request->get['route']);
            } 
        }         
    }
    
	public function indexOld() {
		if (isset($this->request->get['_route_'])) {
			$parts = explode('/', $this->request->get['_route_']);
            var_dump($parts);			
			foreach ($parts as $part) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE keyword = '" . $this->db->escape($part) . "'");
				
				if ($query->num_rows) {
					$url = explode('=', $query->row['query']);
					
					if ($url[0] == 'product_id') {
						$this->request->get['product_id'] = $url[1];
					}
					
					if ($url[0] == 'category_id') {
						if (!isset($this->request->get['path'])) {
							$this->request->get['path'] = $url[1];
						} else {
							$this->request->get['path'] .= '_' . $url[1];
						}
					}	
					
					if ($url[0] == 'manufacturer_id') {
						$this->request->get['manufacturer_id'] = $url[1];
					}
					
					if ($url[0] == 'information_id') {
						$this->request->get['information_id'] = $url[1];
					}	
				} else {
					$this->request->get['route'] = 'error/not_found';	
				}
			}
			
			if (isset($this->request->get['product_id'])) {
				$this->request->get['route'] = 'product/product';
			} elseif (isset($this->request->get['path'])) {
				$this->request->get['route'] = 'product/category';
			} elseif (isset($this->request->get['manufacturer_id'])) {
				$this->request->get['route'] = 'product/manufacturer';
			} elseif (isset($this->request->get['information_id'])) {
				$this->request->get['route'] = 'information/information';
			}
			   
			if (isset($this->request->get['route'])) {
				return $this->forward($this->request->get['route']);
			}
		}
	}
}
?>